/*
 * @Description:输出查询省市县
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-10-19 16:20:45
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-10-23 20:43:32
 */
import { Body, Controller, Get, Query, UseGuards } from '@nestjs/common';
import { BaseAreaService } from './base-area.service'
import {BaseArea} from '../../entity/baseArea.entity'
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';

import { PageNation } from 'src/interface/page.interface';
import { BaseAreaInterfaceCodeId, BaseAreaInterfaceParentId } from 'src/interface/baseArea.interface';
@Controller('baseArea')
export class BaseAreaController {
    constructor(private baseAreaRepository: BaseAreaService) {

    }
    //查找省
    @UseGuards(JwtAuthGuard)
    @Get('findProvince')
    async findProvince(@Query() baseArea: BaseArea): Promise<PageNation>{
        console.log(baseArea)
        if (typeof baseArea.codeid !== 'undefined' ) {
            let params = { ...baseArea };
            params.codeid = Number(params.codeid)
            //codeId从10开始查询的。
            if (baseArea.codeid > 10) {
                const res = await this.baseAreaRepository.findAndCountBy(params)
                console.log('res:',res)
                return res
            } else {
                return {
                    list: [],
                    count:0
                }
            }

        } else {
            return {
                list: [],
                count:0
            }
        }

    }
    //查找省
    @UseGuards(JwtAuthGuard)
    @Get('findProvinceArrayParentId')
    async findProvinceArrayParentId(@Query() baseArea:BaseAreaInterfaceParentId): Promise<PageNation>{
        console.log(baseArea)
        let ids = baseArea?.parentid ?? '[]';
        const idnew = JSON.parse(ids);
         console.log('1-2:',idnew)
          const res = await this.baseAreaRepository.findAndCountByArrayParentId(idnew)
                // console.log('res:',res)
                return res

    }

     //查找省
    @UseGuards(JwtAuthGuard)
    @Get('findProvinceArrayCodeId')
    async findProvinceArrayCodeId(@Query() baseArea:BaseAreaInterfaceCodeId): Promise<PageNation>{
        console.log(baseArea)
        let ids = baseArea?.codeid ?? '[]';
        const idnew = JSON.parse(ids);
         console.log('1-2:',idnew)
          const res = await this.baseAreaRepository.findAndCountByArrayCodeId(idnew)
                // console.log('res:',res)
                return res

    }
    @UseGuards(JwtAuthGuard)
    @Get('findAndAllProvince')
    async findAndAllProvince(): Promise<PageNation>{

          const res = await this.baseAreaRepository.findAndAllProvince()
                // console.log('res:',res)
                return res

    }


    //查找市
    @UseGuards(JwtAuthGuard)
    @Get('findArea')
    async findArea(@Query() baseArea: BaseArea): Promise<PageNation> {
        if (typeof baseArea.parentid !== 'undefined') {
              const res = await this.baseAreaRepository.findAndCountBy(baseArea)
                console.log('res:',res)
                return res
        } else {
            return {
                list: [],
                count:0
            }
        }

     }
    //查找县
    @UseGuards(JwtAuthGuard)
    @Get('findCounty')
    async findCounty(@Query() baseArea: BaseArea): Promise<PageNation> {
        if (typeof baseArea.parentid !== 'undefined') {
            const res = await this.baseAreaRepository.findAndCountBy(baseArea)
            // console.log('res:',res)
            return res
        } else {
            return {
                list: [],
                count:0
            }
        }

    }
}
